/*
https://leetcode-cn.com/problems/number-of-digit-one/solution/shu-zi-1-de-ge-shu-by-leetcode-solution-zopq/
 */
public class Solution233 {
    public int countDigitOne(int n) {
        long t=1;
        int ans=0;
        for (;n>=t;t*=10){
            ans+=n/(t*10)*t+Math.min(Math.max(n%(t*10)-t+1,0),t);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution233().countDigitOne(13));
    }
}
